package com.jxs.android.weixi;

import java.io.IOException;
import java.util.HashMap;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.weiget.CustomerViewItem;
import com.jxs.android.weiget.TableFormView;
import com.jxs.android.weiget.TopBar;
import com.telecom.weixi.R;

@SuppressLint("HandlerLeak")
public class MyCustomer_Customer_View extends AbstractActivity {

	private static HashMap<String, String> keys = new HashMap<String, String>();

	static {
		keys.put("cust_name", "客户名称");
		keys.put("cust_type_name", "客户类型");
		keys.put("custarpu_val", "客户ARPU");
		keys.put("product_info", "产品信息");
		keys.put("income_info", "收入信息");
		keys.put("cust_id", "客户编码");
		keys.put("cust_sub_brand_name", "客户品牌");
		keys.put("cust_addr", "客户地址");
		keys.put("fourth_sale_organise_name", "四级销售组织");

	}

	private String cust_name = "cust_name";// 客户名称
	private String cust_type_name = "cust_type_name";// 客户类型
	private String custarpu_val = "custarpu_val";// 客户ARPU
	private String product_info = "product_info";// 产品信息
	private String income_info = "income_info";// 收入信息
	private String cust_addr = "cust_addr";// 客户地址
	private String fourth_sale_organise_name = "fourth_sale_organise_name";// 四级销售组织
	private String cust_sub_brand_name = "cust_sub_brand_name";// 客户品牌

	private TableFormView tableFormView;
	private LinearLayout listView;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		context = this;
		initTopBar(savedInstanceState, R.layout.bu_my_customer_customer_view);
		listView = (LinearLayout) findViewById(R.id.list_bar);
		tableFormView = new TableFormView(this);
		tableFormView.setColumnStretchable(1, true);
		tableFormView.setColumnShrinkable(2, true);
		// listView.addView(tableFormView);
		String cust_id = "";
		if (null != getIntent() && null != getIntent().getExtras()) {
			cust_id = getIntent().getExtras().getString("cust_id");
		}
		loading(cust_id);
	}

	private ProgressDialog progressDialog = null;

	private void loading(final String cust_id) {
		progressDialog = ProgressDialog.show(this, "", "正在加载，请稍候...", true);
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = WeixiApplication.WSDL_URL;// wsdl
				String namespace = WeixiApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "getCustView";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", WeixiApplication.getUserInfo().getTicket());
				request.addProperty("cust_id", cust_id);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paserLoading(response);
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}
				});
			}
		}).start();
	}

	private void paserLoading(SoapObject soapObject) {
		String stauts = soapObject.getPropertySafelyAsString("status");
		SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
		String message = msgObjec.getPropertySafelyAsString("message");
		if ("-2".equals(stauts)) {
			Message msg = new Message();
			msg.what = ERROR_MSG;
			msg.obj = message;
			handler.sendMessage(msg);
		} else if ("-1".equals(stauts)) {
			Message msg = new Message();
			msg.what = TO_LOGIN;
			msg.obj = message;
			handler.sendMessage(msg);
		} else if ("0".equals(stauts)) {

			SoapObject retMsgObject = (SoapObject) soapObject.getProperty("retMsg");
			Message msg = new Message();
			msg.what = UPDATE_LIST_UI;
			msg.obj = retMsgObject;
			handler.sendMessage(msg);
		}
	}

	private static final int UPDATE_LIST_UI = 0x101;
	private static final int ERROR_MSG = 0x1013;
	private static final int TO_LOGIN = 0x1019;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog) {
				progressDialog.dismiss();
			}
			if (msg.what == UPDATE_LIST_UI) {
				init((SoapObject) msg.obj);
			} else if (msg.what == ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == TO_LOGIN) {
				errorMsgToLogin(msg.obj.toString());
			}
		}

	};

	private void init(SoapObject retMsgObject) {
		if (null != retMsgObject) {

			CustomerViewItem child = new CustomerViewItem(context);
			child.setName(keys.get(cust_name));
			child.setValue(retMsgObject.getPropertySafelyAsString(cust_name));
			child.setBackgroud(CustomerViewItem.BackGroud.HEAD);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(cust_type_name));
			child.setValue(retMsgObject.getPropertySafelyAsString(cust_type_name));
			child.setBackgroud(CustomerViewItem.BackGroud.BODY);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(custarpu_val));
			child.setValue(retMsgObject.getPropertySafelyAsString(custarpu_val));
			child.setBackgroud(CustomerViewItem.BackGroud.BODY);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(product_info));
			child.setValue(retMsgObject.getPropertySafelyAsString(product_info));
			child.setBackgroud(CustomerViewItem.BackGroud.BODY);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(income_info));
			child.setValue(retMsgObject.getPropertySafelyAsString(income_info));
			child.setBackgroud(CustomerViewItem.BackGroud.BODY);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(cust_addr));
			child.setValue(retMsgObject.getPropertySafelyAsString(cust_addr));
			child.setBackgroud(CustomerViewItem.BackGroud.BODY);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(fourth_sale_organise_name));
			child.setValue(retMsgObject.getPropertySafelyAsString(fourth_sale_organise_name));
			child.setBackgroud(CustomerViewItem.BackGroud.BODY);
			listView.addView(child);

			child = new CustomerViewItem(context);
			child.setName(keys.get(cust_sub_brand_name));
			child.setValue(retMsgObject.getPropertySafelyAsString(cust_sub_brand_name));
			child.setBackgroud(CustomerViewItem.BackGroud.TAIL);
			listView.addView(child);

		}

	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindBtn();
		topBarReturn(topBar);
		topBar.setTitle("客户视图");
	}

}
